package com.powerband.testapiandroid

import android.app.DatePickerDialog
import android.app.TimePickerDialog
import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.TimePicker.OnTimeChangedListener
import androidx.activity.result.contract.ActivityResultContract
import androidx.databinding.adapters.TimePickerBindingAdapter
import com.powerband.testapiandroid.databinding.ActivityCommandDemoBinding
import com.powerband.testapiandroid.databinding.ActivityMainBinding
import com.powerband.testapiandroid.databinding.ActivitySubCommandBinding
import java.util.Calendar
import java.util.Date

class SubCommandActivity : AppCompatActivity() {
    private lateinit var binding: ActivitySubCommandBinding
    private var h1 = 0
    private var m1 = 0
    private var h2 = 0
    private var m2 = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivitySubCommandBinding.inflate(layoutInflater)
        setContentView(binding.root)
//        setContentView(R.layout.activity_sub_command)

        binding.textView1.setOnClickListener {
            buttonFunc(it)
        }

        binding.textView2.setOnClickListener {
            buttonFunc(it)
        }

        binding.button1.setOnClickListener {
            val swiV = if (binding.switch2.isChecked) 1 else 0
            setResult(10086, Intent().putExtra("SEL",intArrayOf(swiV, h1, m1, h2, m2)))
            finish()
        }
        binding.button2.setOnClickListener {
            finish()
        }
    }

    fun buttonFunc(view: View) {
        when (view.id) {
            R.id.textView1 -> {
                val datePickerDialog = TimePickerDialog(
                    this,
                    TimePickerDialog.OnTimeSetListener { _, i, i2 ->
                        val mDate = "${i}-${i2}"
                        // 将选择的日期赋值给TextView
                        h1 = i
                        m1 = i2
                        binding.textView1.text = mDate
                    },0,0,true
                )
                datePickerDialog.show()
            }
            R.id.textView2 -> {
                val datePickerDialog = TimePickerDialog(
                    this,
                    TimePickerDialog.OnTimeSetListener { _, i, i2 ->
                        val mDate = "${i}-${i2}"
                        // 将选择的日期赋值给TextView
                        h2 = i
                        m2 = i2
                        binding.textView2.text = mDate
                    },0,0,true
                )
                datePickerDialog.show()
            }
        }
    }

}